package service

import (
	"AiBus/global"
	"AiBus/model"
	"AiBus/request"
	"AiBus/utils"
	"AiBus/utils/mail"
	"fmt"
	"github.com/gin-gonic/gin"
	"sync"
	"time"
)

var wg sync.WaitGroup

/*添加待办*/
func ADDToDoList(c *gin.Context, req request.ToDoReq, UserID string) {
	wg.Add(1)
	go func() {

		defer wg.Done()
	}()
	wg.Wait()

	todo := model.TODO{
		Title:       req.Title,
		Desc:        req.Desc,
		PriorityID:  uint(*req.Priority),
		ToDoClassID: uint(*req.ToDoClass),
		Inform:      *req.Inform,
		StartTime:   req.StartTime,
		OverTime:    req.OverTime,
		Convey:      req.Convey,
		UserId:      UserID,
	}
	tx := global.App.DB.Create(&todo)

	if tx.Error != nil {
		global.App.Log.Error(tx.Error.Error())
		utils.Fail(c, 408, "待办添加失败")
	} else {
		//SendToDoSM
		switch *req.Inform {
		case 0:
			err := mail.SendMail(req.Convey, req)
			if err != nil {
				fmt.Println(err)
				utils.Fail(c, 408, "邮箱配置错误")
			} else {
				utils.Success(c, 0, "添加成功")
			}
		case 1:
			err := utils.SendToDoSM(req.Convey, map[string]string{
				"title":   req.Title,
				"content": req.Desc,
			})
			if err != nil {
				global.App.Log.Error(err.Error())
				utils.Fail(c, 40003, "待办通知发送失败")
			} else {
				utils.Success(c, 0, "添加成功")
			}
		}
	}
}

/*所有未完成待办加载*/
func GetToDoList(c *gin.Context, UserID string) {
	var Count int64
	todo := []model.TODO{}
	tx := global.App.DB.Where("user_id=? and state=0", UserID).Find(&todo).Count(&Count)
	if tx.Error != nil || tx.RowsAffected == 0 {
		utils.Fail(c, 408, "待办添加失败")
		global.App.Log.Error(tx.Error.Error())
	} else {
		utils.Success(c, Count, todo)

	}

}

/*更新待办状态请求*/
func FinishToDo(c *gin.Context, req request.FinishToDoReq, UserID string) {
	todo := model.TODO{}
	tx := global.App.DB.Model(&todo).Where("user_id=? and id=?", UserID, req.ID).Update("state", 1)
	if tx.Error != nil {
		global.App.Log.Error(tx.Error.Error())
		utils.Fail(c, 4000, "待办更新失败")

	} else {
		utils.Success(c, 0, "待办更新成功")
	}

}

func DeleTodo(c *gin.Context, req request.DelToDoReq, UserID string) {
	tx := global.App.DB.Model(&model.TODO{}).Where("user_id=? and id=?", UserID, req.ID).Update("state", 3)

	if tx.Error != nil {
		global.App.Log.Error(tx.Error.Error())
		utils.Fail(c, 4000, "待办取消失败")
	} else {
		utils.Success(c, 0, "待办取消成功")
	}
}

/*加载所有已完成的待办*/
func GetOverToDo(c *gin.Context, UserID string) {
	todo := []model.TODO{}
	var Count int64
	tx := global.App.DB.Where("user_id=? and state=1", UserID).Find(&todo).Count(&Count)
	if tx.Error != nil || tx.RowsAffected == 0 {
		global.App.Log.Error(tx.Error.Error())
		utils.Fail(c, 4000, "待办数据加载失败")

	} else {
		utils.Success(c, Count, todo)
	}
}

/*加载所有用户取消的待办*/
func GetDeleToDo(c *gin.Context, UserID string) {
	todo := []model.TODO{}
	var Count int64
	tx := global.App.DB.Where("user_id=? and state=3", UserID).Find(&todo).Count(&Count)
	if tx.Error != nil || tx.RowsAffected == 0 {
		global.App.Log.Error(tx.Error.Error())
		utils.Fail(c, 4000, "待办数据加载失败")

	} else {
		utils.Success(c, Count, todo)
	}
}

/* 待办状态*/
func ToDoState(c *gin.Context, req request.ToDoState, UserID string) {

	/*
		0：表示正常
			1：表示延期，如果延期则更新结束日期
	*/
	todo := model.TODO{}
	switch req.StateCode {
	case "0":
		tx := global.App.DB.Model(&todo).Where("user_id=?", UserID).Update("deleted_at", time.Now().Format(time.DateTime))
		if tx.Error != nil {
			global.App.Log.Error(tx.Error.Error())
			utils.Fail(c, 409, "待办更新失败")
		} else {
			utils.Success(c, 0, "待办更新成功!!!")
		}
	case "1":
		tx := global.App.DB.Model(&todo).Where("user_id=?", UserID).Update("over_time", req.OverTime)
		if tx.Error != nil {
			global.App.Log.Error(tx.Error.Error())
			utils.Fail(c, 409, "待办更新失败")
		} else {
			utils.Success(c, 0, "待办更新成功!!!")
		}
	default:
		utils.Fail(c, 500, "未知错误")
	}
}
func ToDoUPDATE(c *gin.Context, req request.TODOUPDATEReq, UserID string) {
	todolists := model.TODO{}

	tx := global.App.DB.Model(&todolists).Where("user_id=? and id=? and state = 0", UserID, req.ID).Updates(map[string]interface{}{
		"Title":       req.Title,
		"Desc":        req.Desc,
		"PriorityID":  req.Priority,
		"ToDoClassID": req.ToDoClass,
		"Inform":      req.Inform,
		"Convey":      req.Convey,
		"StartTime":   req.StartTime,
		"OverTime":    req.OverTime,
		"updated_at":  time.Now().Format(time.DateTime),
	})
	if tx.Error != nil || tx.RowsAffected == 0 {
		global.App.Log.Error(tx.Error.Error())
		utils.Fail(c, 4200, "待办更新失败")

	} else {
		utils.Success(c, 1, request.TODOUPDATERes{
			ID:          req.ID,
			Title:       req.Title,
			Desc:        req.Desc,
			PriorityID:  req.Priority,
			ToDoClassID: req.ToDoClass,
			Inform:      req.Inform,
			Convey:      req.Convey,
			StartTime:   req.StartTime,
			OverTime:    req.OverTime,
		})
	}
}

func Concreteness(c *gin.Context, req request.ConcretenessTODOReq, UserID string) {
	todo := model.TODO{}

	var Count int64
	tx := global.App.DB.Where("id=? and user_id=?", req.ID, UserID).Find(&todo).Count(&Count)
	if tx.Error != nil {
		global.App.Log.Error(tx.Error.Error())
		utils.Fail(c, 4000, "待办数据加载失败")

	} else {
		utils.Success(c, Count, todo)
	}
}
